#include <iostream>

#include "util.hpp"
#include "Level.hpp"
#include "message.hpp"
#include "format.hpp"
#include "sink.hpp"
#include "Logger.hpp"

#include "mylog.h"


/*测试工具类*/
void UtilTest()
{
    mian_yang::util::Date::GetTime();

    std::cout << mian_yang::util::File::exists("./test.cc") << std::endl;

    std::cout << mian_yang::util::File::path("./Test/TestUtil") << std::endl;

    mian_yang::util::File::createDirectory("./Test/TestUtil");
}

static void TestLevel()
{
    std::cout << mian_yang::LogLevel::toString(mian_yang::LogLevel::value::DEBUG) << std::endl;
}

void TestFormat()
{
    mian_yang::LogMsg msg("main.cc", 12, "logger", mian_yang::LogLevel::value::DEBUG, "正在进行格式化测试...");
    mian_yang::Formatter fmt;          /*默认构造*/
    std::string str = fmt.format(msg); /*格式化器直接对msg进行格式化*/
    std::cout << str << std::endl;
}

void TestSink()
{
    /*先构造日志信息*/
    mian_yang::LogMsg msg(__FILE__, __LINE__, "logger", mian_yang::LogLevel::value::DEBUG, "正在进行测试...");
    mian_yang::Formatter fmt;          /*创建格式化器*/
    std::string str = fmt.format(msg); /*使用格式化器对日志消息进行格式化*/

    /*创建落地器*/
    mian_yang::LogSink::ptr StdSk_ptr = mian_yang::SinkFactory<mian_yang::StdoutSink>::create();
    mian_yang::LogSink::ptr FileSk_ptr = mian_yang::SinkFactory<mian_yang::FileSink>::create("Test/Logfile/file-");
    mian_yang::LogSink::ptr RollSk_ptr = mian_yang::SinkFactory<mian_yang::RollSink>::create("Test/Logfile/roll-", 1000 * 1000);

    /*创建好落地器后就行数据落地。*/
    int cnt = 1;
    while (cnt <= 10 * 10000)
    {
        std::string tmp = str;
        tmp.pop_back();
        tmp += std::to_string(cnt);
        tmp += "\n";
        FileSk_ptr->log(tmp.c_str(), tmp.size());
        RollSk_ptr->log(tmp.c_str(), tmp.size());
        cnt++;
    }
}

void TestTimeSink()
{
    /*先构造日志信息*/
    mian_yang::LogMsg msg(__FILE__, __LINE__, "logger", mian_yang::LogLevel::value::DEBUG, "正在进行测试...");
    mian_yang::Formatter fmt;          /*创建格式化器*/
    std::string str = fmt.format(msg); /*使用格式化器对日志消息进行格式化*/

    mian_yang::LogSink::ptr TimeSK_ptr = mian_yang::SinkFactory<mian_yang::RollByTimeSink>::create("Test/Logfile/rolltime-", mian_yang::TimeGap::GAP_SECOND);
        /*创建好落地器后就行数据落地。*/
        int cnt = 1;
        while (cnt <= 20)   
        {
            std::string tmp = str;
            tmp.pop_back();
            tmp += std::to_string(cnt);
            tmp += "\n";
            TimeSK_ptr->log(tmp.c_str(), tmp.size());
            cnt++;
            sleep(1);
        }
}

void TestLoggerLS()
{
    /*创建日志器工厂*/
    std::shared_ptr<mian_yang::LoggerBuilder> logger_builder = std::make_shared<mian_yang::LocallLoggerBuilder>();
    logger_builder->buildLoggerName("_root_log");
    logger_builder->buildLoggerType(mian_yang::LoggerType::LOGGER_SYNC);
    logger_builder->buildLoggerLevel(mian_yang::LogLevel::value::DEBUG);
    logger_builder->buildFormatter();
    logger_builder->buildSink<mian_yang::StdoutSink>();
    logger_builder->buildSink<mian_yang::FileSink>("./Test/Logfile/file");    
    logger_builder->buildSink<mian_yang::RollSink>("./Test/Logfile/rollfile-", 1000 * 1000);
    mian_yang::Logger::ptr lpr = logger_builder->build();
    int cnt = 0;
    while (cnt < 30 * 10000)
    {
        lpr->debug(__FILE__, __LINE__, "正在进行测试中%d", cnt++);
    } 
}

void TestLoggerASLS()
{
    std::unique_ptr<mian_yang::LoggerBuilder> Lgbder(new mian_yang::LocallLoggerBuilder()); /*不允许抽象类型创建对象， 那么可以使用抽象指针。*/
    Lgbder->buildLoggerName("async_logger");
    Lgbder->buildLoggerLevel(mian_yang::LogLevel::value::WARN);
    Lgbder->buildFormatter();
    Lgbder->buildLoggerType(mian_yang::LoggerType::LOGGER_ASYNC);
    Lgbder->buildSink<mian_yang::FileSink>("./logfile/test.log/file.log");
    Lgbder->buildSink<mian_yang::RollSink>("./logfile/test.log/roll-", 1024 * 1024);
    mian_yang::Logger::ptr Logger_ptr = Lgbder->build();

    int cnt = 0;
    while (cnt <= 50 * 10000)
    {
        Logger_ptr->fatal(__FILE__, __LINE__, "日志消息——%d", cnt);
        cnt++;
    }
    

}

void TestLogggerASGS()
{
    std::shared_ptr<mian_yang::LoggerBuilder> logbder = std::make_shared<mian_yang::GlobalLoggerBuilder>();
    logbder->buildLoggerName("async_logger");
    logbder->buildLoggerType(mian_yang::LoggerType::LOGGER_ASYNC);
    logbder->buildLoggerLevel(mian_yang::LogLevel::value::WARN);
    logbder->buildFormatter();
    logbder->buildSink<mian_yang::StdoutSink>();
    logbder->buildSink<mian_yang::FileSink>("./Test/Logfile/file");
    logbder->buildSink<mian_yang::RollSink>("./Test/Logfile/roll-", 1024 * 1024);
    mian_yang::Logger::ptr logger = logbder->build();

    int cnt = 0;
    while (cnt < 10000)
    {
        logger->fatal(__FILE__, __LINE__, "日志消息——%d", cnt);
        cnt++;
    } /*就是这样，这里应该打印1w条，但是只有打印3000条，随机的，也肯能是几千条
    。就是老师您看， 我这里应该是打印1w条。但是这里只能打印几千条，打不到1w。对，就是这个*/ 
    // sleep(5);/**/
}

void TestMyLog()
{
    int cnt = 0;
    while (cnt <= 10000)
    {
        DEBUG("正在进行测试....%d", cnt++);
    }
}


int main()
{
    // TestLevel();
    // TestFormat();
    // TestSink();
    // TestTimeSink();
    // TestLoggerASLS();
    // TestLogggerASGS();
    TestMyLog();

    return 0;
}
